define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            Fast.config.openArea = ['95%','95%'];
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/transfer/index' + location.search,
                    add_url: 'course/transfer/add',
                    edit_url: 'course/transfer/edit',
                    del_url: 'course/transfer/del',
                    multi_url: 'course/transfer/multi',
                    import_url: 'course/transfer/import',
                    table: 'course_transfer_school',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'student_id', title: __('Student_id')},
                        // {field: 'order_id', title: __('Order_id')},
                        {field: 'type', title: __('Type'), searchList:Config.typeList,  formatter: Table.api.formatter.label},
                        {field: 'status', title: __('Status'), searchList:Config.statusList,  formatter: Table.api.formatter.label},
                        {field: 'student.name', title: __('Student.name'), operate: 'LIKE'},

                        {field: 'reason', title: __('Reason'), operate: false},
                        {field: 'refuse_content', title: __('RefuseContent'), operate: false},
                        {field: 'old_class_hour', title: __('申请订单信息'), operate: false, formatter: Controller.api.formatter.old_info},
                        {field: 'new_class_hour', title: __('转单信息'), operate: false, formatter: Controller.api.formatter.new_info},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'apply',
                                    text: __('通过'),
                                    title: __('通过'),
                                    refresh:true,
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    visible: function (row) {
                                        if(row.status == 1){
                                            //返回true时按钮显示,返回false隐藏
                                            return true;
                                        }
                                    },
                                    url:function(row){
                                        return 'course/transfer/apply?ids='+row.id;
                                    },
                                },
                                {
                                    name: 'apply',
                                    text: __('拒绝'),
                                    title: __('拒绝'),
                                    refresh:true,
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    visible: function (row) {
                                        if(row.status == 1){
                                            //返回true时按钮显示,返回false隐藏
                                            return true;
                                        }
                                    },
                                    click:function(index,row){
                                        layer.prompt({title: __('请填写拒绝原因'), formType: 2}, function(text, index){
                                            Fast.api.ajax('course/transfer/apply?ids='+row.id+'&apply=refuse&refuse_content='+text,function (data,ret) {
                                                layer.close(index);
                                                table.bootstrapTable('refresh', {});
                                            });
                                        });

                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        apply: function () {
            Controller.api.bindevent();
            $("#c-teacher_id").data("params", function (obj) {
                //obj为SelectPage对象
                if($("#c-school_id").val()){
                    return {custom: {school_id: $("#c-school_id").val()}};
                }

            });
            $("#c-order_goods_id").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {teacher_id: $("#c-teacher_id").val()}};
            });
            $(document).on('click', "[data-toggle='select_classes']", function () {




                var url = "course/course/select_classes?transfer_id="+Config.row.id;

                Fast.api.open(url, '选择班次', {
                    callback: function (res) {
                        $(".classes_div").html("");
                        $.each(res,function(index,items){

                            $(".classes_div").append("<div><span>校区："+items.school_name+"</span>  " +
                                "<span>班次："+items.classes_name+"</span>  " +
                                "<span>时间："+items.time_text+"</span></div>");
                        });
                        $(".config").val(JSON.stringify(res));
                        console.log(res);
                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法

                old_info: function (value, row, index) {
                    var html = "-";
                    if(row.status == 2 ){
                        html =  "老订单校区："+row.school_name +"<br/>" +
                            "老订单课程：" + row.course_name+"<br/>" +
                            "老订单课时：" + row.old_class_hour+"<br/>" +
                            "老订单剩余课时：" + row.old_surplus_class_hour+"<br/>" +
                            "老订单剩余价值：" + row.old_surplus_price+"<br/>";
                    }

                    return html;
                },
                new_info: function (value, row, index) {
                    var html = "-";
                    if(row.status == 2 ){
                        html =  "新订单校区："+row.new_school_name +"<br/>" +
                            "新订单课程：" + row.new_course_name+"<br/>" +
                            "新订单课时：" + row.new_class_hour+"<br/>" +
                            "新订单剩余课时：" + row.new_surplus_class_hour+"<br/>" +
                            "新订单剩余价值：" + row.new_surplus_price+"<br/>";
                    }

                    return html;
                },

            },
        }
    };
    return Controller;
});
